// import { asyncRoutes, constantRoutes } from '@/router'
import { constantRoutes } from '@/router'
import { getMenu } from '@/api/user'
import Layout from '@/layout'

/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */
// function hasPermission(roles, route) {
//   if (route.meta && route.meta.roles) {
//     return roles.some(role => route.meta.roles.includes(role))
//   } else {
//     return true
//   }
// }

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */
// export function filterAsyncRoutes(routes, roles) {
//   const res = []

//   routes.forEach(route => {
//     const tmp = { ...route }
//     if (hasPermission(roles, tmp)) {
//       if (tmp.children) {
//         tmp.children = filterAsyncRoutes(tmp.children, roles)
//       }
//       res.push(tmp)
//     }
//   })

//   return res
// }
function filterAsyncRouter(asyncRouterMap) { // 遍历后台传来的路由字符串，转换为组件对象
  try {
    const accessedRouters = asyncRouterMap.filter(route => {
      if (route.component) {
        if (route.component === 'Layout') { // Layout组件特殊处理
          route.component = Layout
        } else {
          const component = route.component
          route.component = resolve => {
            require(['@/views' + component + '/index.vue'], resolve)
            // require(['@/views' + component], resolve)
          }
        }
        const meta = { title: route.name, icon: route.icon }
        route['meta'] = meta
      }
      if (route.children && route.children.length > 0) {
        // 当设置 noRedirect 的时候该路由在面包屑导航中不可被点击
        route['redirect'] = 'noRedirect'
        route.children = filterAsyncRouter(route.children)
      }
      return true
    })
    return accessedRouters
  } catch (e) {
    console.log(e)
  }
}

const state = {
  routes: [],
  addRoutes: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
  }
}

const actions = {
  // generateRoutes({ commit }, roles) {
  //   return new Promise(resolve => {
  //     let accessedRoutes
  // if (roles.includes('admin')) {
  //   accessedRoutes = asyncRoutes || []
  // } else {
  //   accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
  // }
  //     commit('SET_ROUTES', accessedRoutes)
  //     resolve(accessedRoutes)
  //   })
  // }
  async generateRoutes({ commit }, roles) {
    // 取后台路由

    const asyncRouter = await getMenu()

    return new Promise(resolve => {
      const tmp = asyncRouter.data
      const accessedRoutes = filterAsyncRouter(tmp)
      // const myRoutes = filterAsyncRouter(tmp)
      // let accessedRoutes
      // if (roles.includes('ROLE_ADMIN')) {
      //   accessedRoutes = asyncRoutes || []
      // } else {
      //   accessedRoutes = filterAsyncRouter(tmp)
      // }
      // console.log(myRoutes)
      // console.log(accessedRoutes)
      commit('SET_ROUTES', accessedRoutes)
      resolve(accessedRoutes)
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
